﻿using Pay.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Pay.DTO;
using Pay.TO;
using Pay.Core;

namespace Pay.Token.Controllers {
    [AppAuthorize("Member")]
    public class MerchantBankCardController : GeneralController {

        private IMerchantBankCardService _iMerchantBankCardService;
        public MerchantBankCardController(IMerchantBankCardService iMerchantBankCardService) {
            _iMerchantBankCardService = iMerchantBankCardService;
        }

        /// <summary>
        /// 我的银行卡
        /// </summary>
        /// <returns></returns>
        public ActionResult MyBankCard() {
            var myBankCard = _iMerchantBankCardService.GetBankCardByMerchantId(UserId);
            ViewBag.MyBankCard = myBankCard;
            return View();
        }

        /// <summary>
        /// 编辑银行卡
        /// </summary>
        /// <returns></returns>
        public ActionResult EditBankCard() {
            var myBankCard = _iMerchantBankCardService.GetBankCardByMerchantId(UserId) ?? new api_merchant_bank_card_dto();
            ViewBag.BankList = typeof(BankCode).GetDropdownItemList();
            ViewBag.MyBankCard = myBankCard;
            return View();
        }

        /// <summary>
        /// 上传银行卡
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult UploadHandIdCardImg(edit_merchant_bank_card_dto model) {
            ViewBag.BankCardInfo = model;
            ViewBag.ImgUrl = RequestMethod.token_imgurl;
            return View();
        }

        /// <summary>
        /// 新增或修改银行卡
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddOrUpdateBankCard(edit_merchant_bank_card_dto model) {
            //string path = base.Upload("bankCard", "pz");
            //if (!string.IsNullOrEmpty(path)) {
            //    model.hand_id_card_img = path;
            //}
            //if (string.IsNullOrEmpty(model.hand_id_card_img)) {
            //    throw new ServerErrorException("请上传手持身份证照片");
            //}
            if (!ModelState.IsValid)
                return Json(ResultToJson.Json<bool>(ModelError));
            var result = _iMerchantBankCardService.AddOrUpdateBankCard(model, UserId);
            if (result) {
                return Json(ResultToJson.Json("操作成功", true), JsonRequestBehavior.AllowGet);
            } else {
                return Json(ResultToJson.Json("操作失败", false), JsonRequestBehavior.AllowGet);
            }
        }

        /// <summary>
        /// 删除银行卡
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult RemoveBankCard(long id) {
            var result = _iMerchantBankCardService.RemoveBankCard(id);
            if (result) {
                return Json(ResultToJson.Json("操作成功", true), JsonRequestBehavior.AllowGet);
            } else {
                return Json(ResultToJson.Json("操作失败", false), JsonRequestBehavior.AllowGet);
            }
        }
    }
}